// firstPackage/pages/videoPlay/videoPlay.js

import {
  queryVideoDetail,
  queryVideoCommentList,
  createComment,
  starComment
} from "../../../service/video_service"
import debounce from "../../../tools/debounce"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowCommentPop: false,
    videoId: null,
    videoTag: "新房知识",
    page: 0,
    pageSize: 50,
    createCommentData: {
      content: "",
      videoId: "",
    },
    commentList: [

    ],
    isApprove: false,
    videoData: {},
    videoUrl: "https://imgs.schzh.com/house_video/1634179740020-实体样板间.mp4",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      videoId: options.videoId,
      videoTag: options.tag,
      'createCommentData.videoId': options.videoId
    })
    this.queryVideoDetail();
    // this.queryVideoCommentList();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 获取视频信息：
  queryVideoDetail() {
    wx.showLoading();
    queryVideoDetail(this.data.videoId).then(res => {
      wx.hideLoading();
      console.log("JJLog:视频详情:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          videoData: res.data
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryVideoDetail", err);
    })
  },

  // 获取视频评论列表：
  queryVideoCommentList() {
    wx.showLoading();
    queryVideoCommentList(this.data.videoId, this.data.page, this.data.pageSize).then(res => {
      wx.hideLoading();
      console.log("JJLog:视频评论列表:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          commentList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryVideoCommentList", err);
    })
  },

  showCommentPopAction() {
    this.setData({
      isShowCommentPop: true,
    })
  },
  // 消失
  hideCommentPop() {
    this.setData({
      isShowCommentPop: false,
    })
  },

  //确认发送评论:

  commentSend(e) {
    console.log("JJLog:评论信息是:" + JSON.stringify(e));
    let comment = e.detail.comment;
    this.setData({
      'createCommentData.content': comment,

    })
    debounce(this.sendCommentRequest);
  },
  sendCommentRequest() {
    wx.showLoading();
    createComment(this.data.createCommentData).then(res => {
      wx.hideLoading();
      console.log("JJLog:添加评论成功:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          isShowCommentPop: false
        })
        this.queryVideoCommentList();
      }
    }).catch(err => {
      console.error("JJLog:创建评论失败:" + JSON.stringify(err));
      wx.hideLoading();
      wx.showToast({
        title: err || "评论失败",
        icon: "none"
      })
    })
  },
  // 视频点赞:
  approveCommentAction(e) {
    if (this.data.isApprove) {
      wx.showToast({
        title: '您已经点过赞了',
        icon: "none"
      })
      return;
    }
    let id = e.detail.id;
    let index = e.detail.index;
    let approveNum = this.data.commentList[index].star + 1;
    wx.showLoading();
    starComment(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        let item = 'commentList[' + index + '].star';
        let approve = 'commentList[' + index + '].isApprove';
        this.setData({
          [item]: approveNum,
          [approve]: true,
          isApprove: true,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "点赞失败",
        icon: "none"
      })
    })
  },

})